STACK SEGMENT PARA STACK
        DW 128H DUP(0)
STACK ENDS
              
DATA SEGMENT
	LED	DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H
		DB 7FH,67H,77H,7CH,39H,5EH,79H,71H

        LOSER   DB 'GAME OVER! PRESS ANY KEY TO EXIT...','$'
        BLANK   DB ' ',0AH,0DH,'$'

        WALL0	DW 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
		DW 16,17,18,19,20,22,23,24,25,26,27,28,29,30,31,32
		DW 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48
		DW 49,50,51,52,53,54,55,56,57,59,60,61,62,63,64,65
		DW 66,67,68,69,70,71,72,73,74,75,76,77,78,256,335
		DW 512,591,768,847,1024,1054,1055,1056,1071,1072,1073,1103,1280,1310,1329,1359
		DW 1536,1566,1585,1615,1792,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821
		DW 1822,1841,1842,1843,1844,1845,1846,1847,1848,1849,1850,1851,1852,2048,2087,2088
		DW 2127,2304,2343,2344,2383,2560,2570,2571,2572,2599,2600,2627,2628,2629,2639,2816
		DW 2826,2828,2855,2856,2883,2885,2895,3110,3111,3112,3113,3151,3328,3338,3340,3367
		DW 3368,3395,3397,3407,3584,3594,3595,3596,3623,3624,3651,3652,3653,3663,3840,3879
		DW 3880,3919,4096,4135,4136,4175,4352,4371,4372,4373,4374,4375,4376,4377,4378,4379
		DW 4380,4381,4382,4401,4402,4403,4404,4405,4406,4407,4408,4409,4410,4411,4412,4608
		DW 4638,4657,4687,4864,4894,4913,4943,5120,5150,5151,5152,5167,5168,5169,5199,5376
		DW 5455,5632,5711,5888,5967,6145,6146,6147,6148,6149,6150,6151,6152,6153,6154
		DW 6155,6156,6157,6158,6159,6160,6161,6162,6163,6164,6165,6166,6167,6168,6169,6170
		DW 6172,6173,6174,6175,6176,6177,6178,6179,6180,6181,6182,6183,6184,6185,6186,6187
		DW 6188,6189,6190,6191,6192,6193,6194,6195,6197,6198,6199,6200,6201,6202,6203,6204
		DW 6205,6206,6207,6208,6209,6210,6211,6212,6213,6214,6215,6216,6217,6218,6219,6220
		DW 6221,6222	;TOTAL=303

	WALL1	DW 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
		DW 16,17,18,19,20,22,23,24,25,26,27,28,29,30,31,32
		DW 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48
		DW 49,50,51,52,53,54,55,56,57,59,60,61,62,63,64,65
		DW 66,67,68,69,70,71,72,73,74,75,76,77,78,256,335
		DW 512,591,768,847,1024,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053
		DW 1054,1055,1056,1057,1058,1059,1060,1061,1062,1065,1066,1067,1068,1069,1070,1071
		DW 1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1103,1280,1299
		DW 1340,1359,1536,1555,1596,1615,1792,1811,1852,2048,2067,2087,2088,2108,2127,2304
		DW 2323,2343,2344,2364,2383,2560,2567,2571,2579,2599,2600,2620,2628,2632,2639,2816
		DW 2824,2826,2835,2855,2856,2876,2885,2887,2895,3081,3102,3103,3104,3119,3120,3121
		DW 3151,3328,3336,3338,3347,3367,3368,3388,3397,3399,3407,3584,3591,3595,3603,3623
		DW 3624,3644,3652,3656,3663,3840,3859,3879,3880,3900,3919,4096,4115,4135,4136,4156
		DW 4175,4352,4371,4412,4608,4627,4668,4687,4864,4883,4884,4885,4886,4887,4888,4889
		DW 4890,4891,4892,4893,4894,4895,4896,4897,4898,4899,4900,4901,4902,4905,4906,4907
		DW 4908,4909,4910,4911,4912,4913,4914,4915,4916,4917,4918,4919,4920,4921,4922,4923
		DW 4924,4943,5120,5199,5376,5455,5632,5711,5888,5967,6145,6146,6147,6148,6149
		DW 6150,6151,6152,6153,6154,6155,6156,6157,6158,6159,6160,6161,6162,6163,6164,6165
		DW 6166,6167,6168,6169,6170,6172,6173,6174,6175,6176,6177,6178,6179,6180,6181,6182
		DW 6183,6184,6185,6186,6187,6188,6189,6190,6191,6192,6193,6194,6195,6197,6198,6199
		DW 6200,6201,6202,6203,6204,6205,6206,6207,6208,6209,6210,6211,6212,6213,6214,6215
		DW 6216,6217,6218,6219,6220,6221,6222	;TOTAL=340
     
        RANTIME DW 0

        SNAKE   DW 8000 DUP(0)
        HEAD    DW 0
        TAIL    DW 0
        SLEN    DW 30
        HEALTH  DW 3
	STEPNUM	DW 0
	EATMNUM	DB 0

        DIR     DB '>'
	FACE	DB 0

        DELAY   DW 0
        DELINIT DW 0

        APOS	DW 3083,3142
	HPOS	DW 0,3140
	MPOS	DW 1,3105,3106,3107,3108,3109,3110,3111,3112,3113,3114,3115,3116,3117,3118
	MPOSDIR	DW 0,0,0

	NEEDCKM	DB 0

        HEALSW  DW 0,1,3,7,0FH,1FH,3FH,7FH,0FFH

        ALLOWI  DB 0

        DIFF    DB 0
        HINT    DB 'Select Difficulty:',0AH,0DH,'<E>Easy    <N>Normal    <H>Hard',0AH,0DH,'$'
	CHEAT	DB 1

	SCOREL	DW 0
	SCOREH	DW 0
	MSCORE	DW 2
        SHOWOD  DB 0

DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX

        LEA DX,HINT
        MOV AH,9
        INT 21H

WAITFORDIFF:
        MOV AH,1
        INT 16H
        JZ WAITFORDIFF
        MOV AH,1
        INT 16H
        CMP AL,'T'
        JE CHEATING
        CMP AL,'t'
        JE CHEATING
        CMP AL,'E'
        JE EASYMODE
        CMP AL,'e'
        JE EASYMODE
        CMP AL,'N'
        JE NORMALMODE
        CMP AL,'n'
        JE NORMALMODE
        CMP AL,'H'
        JE HARDMODE
        CMP AL,'h'
        JE HARDMODE
	MOV CHEAT,0
CONTINUEWAIT:
        MOV AH,0CH
        MOV AL,0
        INT 21H
        JMP WAITFORDIFF
CHEATING:
	CMP CHEAT,0
	JE CONTINUEWAIT
	INC CHEAT
	JMP CONTINUEWAIT


EASYMODE:
        MOV HEALTH,4
        JMP BEGINGAME
NORMALMODE:
        MOV HEALTH,3
        MOV DELINIT,40
        JMP BEGINGAME
HARDMODE:
        MOV HEALTH,2
        MOV DELINIT,80

BEGINGAME:
        CMP CHEAT,7
        JNE NOTCHEAT1
        MOV HEALTH,10
NOTCHEAT1:
        MOV DX,0E483H
        MOV AL,36H
        OUT DX,AL

        MOV DX,0E480H
        MOV AX,25
        OUT DX,AL
        MOV AL,AH
        OUT DX,AL


        MOV DX,0E483H
        MOV AL,76H
        OUT DX,AL

        MOV DX,0E481H
        MOV AX,20
        OUT DX,AL
        MOV AL,AH
        OUT DX,AL
        

        MOV AX,CS
        MOV DS,AX

        MOV DX,OFFSET IRQ
        MOV AX,250EH
        INT 21H
        CLI
        MOV DX,0EC4CH
        MOV AL,43H
        OUT DX,AL
        INC DX
        MOV AL,1DH
        OUT DX,AL
        IN AL,21H
        AND AL,10111111B
        OUT 21H,AL

        MOV AX,DATA
        MOV DS,AX

        MOV DX,0E48BH
        MOV AL,80H
        OUT DX,AL

        MOV DX,0E48AH
        MOV AL,0
        OUT DX,AL

        MOV [SNAKE],3073
        MOV 2[SNAKE],3072
        MOV 4[SNAKE],3072
        MOV 6[SNAKE],3072
        MOV 8[SNAKE],3072
        MOV 10[SNAKE],3072
        MOV 12[SNAKE],3072
        MOV 14[SNAKE],3072
        MOV 16[SNAKE],3072
        MOV 18[SNAKE],3072
        MOV 20[SNAKE],3072
        MOV 22[SNAKE],3072
        MOV 24[SNAKE],3072
        MOV 26[SNAKE],3072
        MOV 28[SNAKE],3072

	MOV AX,3
	INT 10H
        MOV CX,2000H
        MOV AH,1
        INT 10H

	MOV BX,4
	CALL RAND
	SHR AL,1
	MOV FACE,AL

	CALL CLRSCR
	CALL DRAWFACE
	MOV DX,3072
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,'O'
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H
	MOV DX,3073
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,'>'
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H

        MOV AH,86H
        MOV CX,40
        MOV DX,0
        INT 15H

RUN:
	CMP CHEAT,7
	JNE NOTCHEAT2
	MOV HEALTH,10
NOTCHEAT2:
	INC STEPNUM
	MOV AX,SNAKE
	MOV HEAD,AX
	LEA SI,SNAKE
	ADD SI,SLEN
	SUB SI,2
	MOV AX,[SI]
	MOV TAIL,AX

	LEA SI,SNAKE
	ADD SI,2
	MOV CX,2
CHECKBITE:
	MOV DX,[SI]
	ADD SI,2
	CMP HEAD,DX
	JNE NOTBITE
	CALL GAMEOVER
NOTBITE:
	ADD CX,2
	CMP CX,SLEN
	JNE CHECKBITE

        MOV CX,DELINIT
        MOV DELAY,CX
        CALL SHOWHEALTH
WAITSOON:
    CALL SHOWSCORE   
        MOV BL,0
        MOV BH,1
        MOV ALLOWI,1
        STI
WAITIRQ:
        CMP BH,0
        JNE WAITIRQ
        CLI
        MOV ALLOWI,0
       ; MOV AH,86H
       ; MOV CX,0
       ; MOV DX,4000
       ; INT 15H

	MOV AH,1
	INT 16H
	JZ PRESSNO

	CMP AL,27
	JNE NEXTJDG1
	CALL EXIT
NEXTJDG1:
        CMP AL,'a'
        JE TOLEFT
        CMP AL,'A'
        JNE NEXTJDG2
TOLEFT:
        CMP DIR,'>'
        JE CLEARBUF
        MOV DIR,'<'
        JMP CLEARBUF
NEXTJDG2:
        CMP AL,'w'
        JE TOUP
        CMP AL,'W'
        JNE NEXTJDG3
TOUP:
        CMP DIR,'V'
        JE CLEARBUF
        MOV DIR,'^'
        JMP CLEARBUF
TOWAIT:
	JMP WAITSOON
NEXTJDG3:
        CMP AL,'d'
        JE TORIGHT
        CMP AL,'D'
        JNE NEXTJDG4
TORIGHT: 
        CMP DIR,'<'
        JE CLEARBUF
        MOV DIR,'>'
        JMP CLEARBUF
NEXTJDG4:
        CMP AL,'s'
        JE TODOWN
        CMP AL,'S'
        JNE JDGPAUSE
TODOWN:
        CMP DIR,'^'
        JE CLEARBUF
        MOV DIR,'V'
        JMP CLEARBUF
JDGPAUSE:
	CMP AL,' '
	JNE CLEARBUF
	CALL PAUSE
	JMP PRESSNO
CLEARBUF:
        MOV AH,0CH
        MOV AL,0
        INT 21H
	JMP CTN

PRESSNO:
        INC DELAY
        CMP DELAY,220
        JNE TOWAIT
	JMP CTN

CTN:    MOV AL,DIR
        CMP AL,'^'
        JNE CMP2
        SUB [HEAD],256
CMP2:   CMP AL,'V'
        JNE CMP3
        ADD [HEAD],256
CMP3:   CMP AL,'>'
        JNE CMP4
        ADD [HEAD],1
CMP4:   CMP AL,'<'
        JNE OUTOFCMP
        SUB [HEAD],1

	JMP OUTOFCMP


TORUN:
	JMP RUN



OUTOFCMP:
	MOV CX,SLEN
	SUB CX,2
MOVE:
	
	LEA SI,SNAKE
        ADD SI,CX
        SUB SI,2
        MOV DX,[SI]
        ADD SI,2
        MOV [SI],DX


        SUB CX,2
        CMP CX,0
        JNE MOVE

        MOV BX,HEAD
        MOV [SNAKE],BX
	
	MOV DX,HEAD
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,DIR
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H

	LEA SI,SNAKE
	ADD SI,2
	MOV DX,[SI]
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,'O'
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H

	MOV AX,HPOS
	MOV CL,FACE
	MOV CH,0
	CMP AX,CX
	JNE NOTEATHEALTH
	LEA SI,HPOS
	ADD SI,2
	MOV CX,[SI]
	CMP CX,HEAD
	JNE NOTEATHEALTH
	INC HEALTH
	MOV [SI],0FFFFH

        MOV DI,1000
        MOV BX,10
        CALL SOUND


        JMP NOTTOOTHERFACE


NOTEATHEALTH:


	CMP STEPNUM,100
	JNE NEXTCMPSTEP
	
	LEA SI,HPOS
	ADD SI,2
	MOV DX,[SI]
	CMP DX,0FFFFH
	JE NEXTCMPSTEP

	MOV [SI],0FFFFH
	MOV CX,HPOS
	CMP CL,FACE
	JNE NEXTCMPSTEP

	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,' '
	MOV BL,0
	MOV AH,9
	MOV CX,1
	INT 10H

NEXTCMPSTEP:

	CMP STEPNUM,200
	JL AFTERCMPSTEP
        MOV STEPNUM,0
	CALL NEWHEALTH

AFTERCMPSTEP:	
	LEA SI,APOS
        MOV CL,FACE
        MOV CH,0
	ADD CX,CX
	ADD SI,CX
	MOV DX,[SI]
	CMP HEAD,DX
	JNE NOTEATAPPLE


	INC SCOREL
	CMP SCOREL,999
	JLE NOTINCSCOREH1

	SUB SCOREL,1000
	INC SCOREH

NOTINCSCOREH1:

	LEA SI,SNAKE
        MOV CX,SLEN
        ADD SI,CX
        MOV AX,TAIL
        MOV [SI],AX
        ADD SLEN,2
		CMP SLEN,4000
		JG NOTPROLONG1

	ADD SI,2
	MOV [SI],AX
	ADD SLEN,2
NOTPROLONG1:
	CALL NEWAPPLE

        MOV DI,1250
        MOV BX,10
        CALL SOUND

	JMP TORUN

NOTEATAPPLE:

	MOV AH,0
	MOV AL,FACE
	CMP AX,MPOS
        JNE TONOTEATM
	LEA SI,MPOS
	MOV CX,0
CMPEATM:
	ADD SI,2
	MOV DX,[SI]
	CMP DX,HEAD
	JE EATINGM
	INC CX
	CMP CX,14
	JNE CMPEATM
TONOTEATM:
	JMP NOTEATM
EATINGM:

        MOV AL,EATMNUM
        MOV AH,0
        MOV DI,AX
        ADD DI,DI
        ADD DI,DI
        ADD DI,DI
        ADD DI,DI
        ADD DI,DI
        ADD DI,DI
        ADD DI,DI
        ADD DI,DI
        ADD DI,1500
        MOV BX,10
        CALL SOUND



        ADD EATMNUM,1
	CMP EATMNUM,14
	JNE TONOTTOOTHERFACE

	MOV CX,MSCORE
	ADD SCOREL,CX
	CMP SCOREL,999
	JLE NOTINCSCOREH2

	SUB SCOREL,1000
	INC SCOREH

NOTINCSCOREH2:

	CMP MSCORE,1000
	JGE NOTADDMSCORE

	ADD MSCORE,2

NOTADDMSCORE:	
	LEA SI,SNAKE
        MOV CX,SLEN
        ADD SI,CX
        MOV AX,TAIL
        MOV [SI],AX
        ADD SLEN,2

	ADD SI,2
	MOV [SI],AX
	ADD SLEN,2

	CMP SLEN,4000
	JG NOTPROLONG2	

	ADD SI,2
	MOV [SI],AX
	ADD SLEN,2
	ADD SI,2
	MOV [SI],AX
	ADD SLEN,2
	ADD SI,2
	MOV [SI],AX
	ADD SLEN,2

NOTPROLONG2:

	MOV EATMNUM,0
	CALL NEWM

	JMP TORUN
TONOTTOOTHERFACE:
	JMP NOTTOOTHERFACE

NOTEATM:
	CMP EATMNUM,0
	JE NOTCHANGEMSCORE
	MOV MSCORE,2

NOTCHANGEMSCORE:
	MOV EATMNUM,0
	MOV AL,FACE
	MOV CX,0
	MOV DX,604
	CMP AL,1
	JNE CALLBISEARCHHEAD
	MOV DX,678
	JMP CALLBISEARCHHEAD



TORUN2:
	JMP TORUN

CALLBISEARCHHEAD:
	MOV BX,HEAD
	CALL BISEARCH
	CMP AX,0FFFFH
	JE NOTTOUCHWALL
	DEC HEALTH
        MOV DI,400
        MOV BX,10
        CALL SOUND
        CMP HEALTH,0
	JNE NOTTOUCHWALL
	CALL GAMEOVER

NOTTOUCHWALL:	

	MOV DX,HEAD
	CMP DH,0
	JE TOOTHERFACE
	CMP DH,24
	JE TOOTHERFACE
	CMP DL,0
	JE TOOTHERFACE
	CMP DL,79
	JE TOOTHERFACE
	JMP NOTTOOTHERFACE
TOOTHERFACE:

	MOV AX,HEAD
	MOV CX,0
	LEA SI,SNAKE
TOAPOINT:
	MOV [SI],AX
	ADD SI,2
	ADD CX,2
	CMP CX,SLEN
	JNE TOAPOINT

	MOV AL,FACE
	MOV BL,1
	SUB BL,AL
	MOV FACE,BL

	CALL DRAWFACE
	
	CMP DIR,'V'
	JE TURNUP
	CMP DIR,'^'
	JE TURNDOWN
	CMP DIR,'<'
	JE TURNRIGHT
	JMP TURNLEFT
TURNUP:
	MOV DIR,'^'
	SUB HEAD,256
	JMP AFTERTURN
TURNDOWN:
	MOV DIR,'V'
	ADD HEAD,256
	JMP AFTERTURN
TURNRIGHT:
	MOV DIR,'>'
	INC HEAD
	JMP AFTERTURN
TURNLEFT:
	MOV DIR,'<'
	DEC HEAD
	JMP AFTERTURN

TORUN3:
	JMP TORUN2

AFTERTURN:
	MOV DX,SNAKE
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,DIR
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H

	MOV BX,2[SNAKE]
	MOV CX,0
	MOV DX,604
	CMP FACE,1
	JNE ATFACE0
	MOV DX,678
ATFACE0:
	CALL BISEARCH
	CMP AX,0FFFFH
	JE NOTHIT
	DEC HEALTH
	CMP HEALTH,0
	JNE NOTHIT
	CALL GAMEOVER
	
NOTHIT:
	MOV AH,86H
	MOV CX,0
	MOV DX,40000
	INT 15H

	MOV AX,HEAD
	MOV [SNAKE],AX
	MOV DX,SNAKE
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,DIR
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H

	LEA SI,SNAKE
	ADD SI,2
	MOV DX,[SI]
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,'O'
	MOV CX,1
	MOV AH,9
	MOV BL,0AH
	INT 10H
TORUN4:
	JMP TORUN3

NOTTOOTHERFACE:
	LEA SI,SNAKE
	ADD SI,SLEN
	SUB SI,2
	MOV DX,[SI]
	CMP DX,TAIL
	JE TORUN4

	MOV AH,0
	MOV AL,FACE
	CMP MPOS,AX
	JNE TAILATWALL
	LEA SI,MPOS
	MOV CX,0
LOOPTAILATM:
	ADD SI,2
	MOV DX,[SI]
	CMP TAIL,DX
	JE TAILISATM
	INC CX
	CMP CX,14
	JNE LOOPTAILATM
	JMP TAILATWALL

TAILISATM:
	MOV BH,0
	MOV AH,2
	MOV DX,TAIL
	INT 10H
	MOV AL,'M'
	MOV CX,1
	MOV AH,9
	MOV BL,9
	INT 10H

	JMP TORUN3


TAILATWALL:
	MOV AL,FACE
	MOV CX,0
	MOV DX,604
	CMP AL,1
	JNE CALLBISEARCHTAIL
	MOV DX,678
	JMP CALLBISEARCHTAIL

CALLBISEARCHTAIL:
	MOV BX,TAIL
	CALL BISEARCH
	CMP AX,0FFFFH
	JE ERASETAIL

	MOV DX,TAIL
	CMP DL,0
	JE PRINTSYMBOL1
	CMP DL,79
	JE PRINTSYMBOL1
	CMP DH,0
	JE PRINTSYMBOL2
	CMP DH,24
	JE PRINTSYMBOL2
	MOV AL,'*'
	JMP PRINTTAIL
PRINTSYMBOL1:
	MOV AL,'|'
	JMP PRINTTAIL
PRINTSYMBOL2:
	MOV AL,'-'
PRINTTAIL:
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV CX,1
	MOV AH,9
	MOV BL,0FH
	INT 10H
TOTORUN3:
	JMP TORUN3 

ERASETAIL:
	MOV DX,TAIL
        CMP HEAD,DX
	JE TOTORUN3

	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AL,' '
	MOV CX,1
	MOV AH,9
	MOV BL,0
	INT 10H	


	JMP TOTORUN3
	

WAITKEY:
        MOV AH,1
        INT 16H
        JZ WAITKEY


        MOV AH,4CH
        INT 21H


CLRSCR PROC NEAR
        MOV DX,OFFSET BLANK
	MOV AH,9
	MOV CL,0
OUTPUTBLANKLINE:
	INT 21H
	INC CL
	CMP CL,27
	JNE OUTPUTBLANKLINE
	RET
CLRSCR ENDP


DRAWFACE PROC NEAR
	MOV DX,0
CLEARC:
	MOV BX,0
	MOV AH,2
	INT 10H
	MOV AH,9
	MOV AL,' '
	MOV CX,80
	INT 10H
	ADD DX,256
	CMP DX,6400
	JNE CLEARC
	MOV AL,FACE
	CMP AL,1
	JE FACE1
FACE0:
	MOV CX,0
WALL0LP:
	MOV BH,0
	MOV AH,2
	LEA SI,WALL0
	ADD SI,CX
	MOV DX,[SI]
	INT 10H
	CMP DL,0
	JE CHANGESTYLE1
	CMP DL,79
	JE CHANGESTYLE1
	CMP DH,0
	JE CHANGESTYLE2
	CMP DH,24
	JE CHANGESTYLE2
	MOV DL,'*'
	JMP NOTCHANGESTYLE0
CHANGESTYLE1:
	MOV DL,'|'
	JMP NOTCHANGESTYLE0
CHANGESTYLE2:
	MOV DL,'-'
NOTCHANGESTYLE0:
	MOV AL,DL
	MOV AH,9
	MOV BL,0FH
	MOV DX,CX
	MOV CX,1
	INT 10H
	MOV CX,DX
        ADD CX,2
        CMP CX,606
        JNE WALL0LP
	JMP DRAWAPPLE

FACE1:
	MOV CX,0
WALL1LP:
	MOV BH,0
	MOV AH,2
	LEA SI,WALL1
	ADD SI,CX
	MOV DX,[SI]
	INT 10H
	CMP DL,0
	JE CHANGESTYLE3
	CMP DL,79
	JE CHANGESTYLE3
	CMP DH,0
	JE CHANGESTYLE4
	CMP DH,24
	JE CHANGESTYLE4
	MOV DL,'*'
	JMP NOTCHANGESTYLE1
CHANGESTYLE3:
	MOV DL,'|'
	JMP NOTCHANGESTYLE1
CHANGESTYLE4:
	MOV DL,'-'
NOTCHANGESTYLE1:
	MOV AL,DL
	MOV AH,9
	MOV BL,0FH
	MOV DX,CX
	MOV CX,1
	INT 10H
	MOV CX,DX
        ADD CX,2
        CMP CX,680
        JNE WALL1LP
	JMP DRAWAPPLE

DRAWAPPLE:
	MOV AL,FACE
	ADD AL,AL
	MOV AH,0
	LEA SI,APOS
	ADD SI,AX
	MOV DX,[SI]
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AH,9
	MOV AL,'a'
	MOV BH,0
	MOV BL,0CH
	MOV CX,1
	INT 10H

	MOV AX,HPOS
	MOV BL,FACE
	CMP AL,BL
	JNE DRAWNEXT
	LEA SI,HPOS
	ADD SI,2
	MOV DX,[SI]
	CMP DX,0FFFFH
	JE DRAWNEXT
	MOV AH,2
	MOV BH,0
	INT 10H
	MOV AH,9
	MOV AL,'H'
	MOV BH,0
        MOV BL,0EH
	MOV CX,1
	INT 10H


DRAWNEXT:
	MOV AX,MPOS
	MOV BL,FACE
	CMP AL,BL
	JE DRAWM
	RET
DRAWM:
	LEA SI,MPOS
	MOV CX,2
LOOPDRAWM:
	ADD SI,2
	MOV DX,[SI]
	MOV AH,2
	MOV BH,0
	INT 10H
	MOV AH,9
	MOV AL,'M'
	MOV BL,9
	MOV DX,CX
	MOV CX,1
	INT 10H
	MOV CX,DX
	ADD CX,2
	CMP CX,30
	JNE LOOPDRAWM
	
	RET

DRAWFACE ENDP


BISEARCH PROC NEAR
	MOV AX,CX
	ADD AX,DX
	SHR AX,1
	SHR AX,1
	SHL AX,1
	LEA SI,WALL0
	CMP FACE,1
	JNE GOTOCMP
	LEA SI,WALL1
GOTOCMP:
	ADD SI,AX
	MOV AX,[SI]
	CMP CX,DX
	JE LASTJDG
	CMP AX,BX
	JE RETSI
	CMP AX,BX
	JL CHANGECX
	CMP AX,BX
	JG CHANGEDX
LASTJDG:
	CMP AX,BX
	JE FIND
	MOV AX,0FFFFH
	RET
FIND:
	MOV AX,CX
	RET
RETSI:
	MOV AX,CX
	ADD AX,DX
	SHR AX,1
	SHR AX,1
	SHL AX,1
	RET
CHANGECX:
	MOV AX,CX
	ADD AX,DX
	SHR AX,1
	SHR AX,1
	SHL AX,1
	MOV CX,AX
	ADD CX,2
	CALL BISEARCH
	RET
CHANGEDX:
	MOV AX,CX
	ADD AX,DX
	SHR AX,1
	SHR AX,1
	SHL AX,1
	MOV DX,AX
	CALL BISEARCH
	RET
BISEARCH ENDP

NEWAPPLE PROC NEAR
BEGINNEW:
	MOV BX,21
	CALL RAND
	MOV CX,AX
	MOV CH,CL
	ADD CH,2
	MOV BX,76
	CALL RAND
	MOV CL,AL
	ADD CL,2
	MOV BH,0
	MOV BL,FACE
	MOV NEEDCKM,1
	CALL CHECKPOS
	CMP AX,0
	JE BEGINNEW
	MOV AL,FACE
	MOV AH,0
	ADD AX,AX
	LEA SI,APOS
	ADD SI,AX
	MOV [SI],CX
	MOV DX,CX
	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AH,9
	MOV AL,'a'
	MOV BH,0
	MOV BL,0CH
	MOV CX,1
	INT 10H
	RET
NEWAPPLE ENDP

NEWHEALTH PROC NEAR
BEGINNEWH:
	MOV BX,21
	CALL RAND
	MOV CX,AX
	MOV CH,CL
	ADD CH,2
	MOV BX,76
	CALL RAND
	MOV CL,AL
	ADD CL,2
	MOV BX,2
	CALL RAND
	MOV BX,AX
	MOV NEEDCKM,1
	CALL CHECKPOS
	CMP AX,0
	JE BEGINNEWH

	MOV [HPOS],BX
	MOV 2[HPOS],CX
        CMP BL,FACE
	JNE NOTDRAW
	MOV DX,CX

	MOV BH,0
	MOV AH,2
	INT 10H
	MOV AH,9
	MOV AL,'H'
	MOV BH,0
	MOV BL,0EH
	MOV CX,1
	INT 10H
NOTDRAW:
	RET
NEWHEALTH ENDP	

NEWM PROC NEAR
BEGINNEWM:
	MOV BH,0
	MOV BL,FACE
	INC BL
	AND BL,1

	LEA SI,MPOS
	MOV [SI],BX
	ADD SI,2

	MOV CX,BX
	MOV BX,16
	CALL RAND
	SHR AX,1
	AND AX,3
	MOV [MPOSDIR],AX
SECONDDIR:
	MOV BX,16
	CALL RAND
	SHR AX,1
	AND AX,3
	LEA SI,MPOSDIR
	MOV DX,[SI]
	XOR DX,AX
	CMP DX,2
	JE TURNADIR2
	JMP NOTTURNADIR2
TURNADIR2:
	INC AX
	AND AX,3
NOTTURNADIR2:
	MOV 2[MPOSDIR],AX
THIRDDIR:
	MOV BX,16
	CALL RAND
	SHR AX,1
	AND AX,3
        LEA SI,MPOSDIR
        ADD SI,2
	MOV DX,[SI]
	XOR DX,AX
	CMP DX,2
	JE TURNADIR3
	JMP NOTTURNADIR3
TURNADIR3:
	INC AX
	AND AX,3
NOTTURNADIR3:
	MOV 4[MPOSDIR],AX
	PUSH CX

	MOV BX,21
	CALL RAND
	MOV CX,AX
	MOV CH,CL
	ADD CH,2
	MOV BX,76
	CALL RAND
	MOV CL,AL
	ADD CL,2
	LEA SI,MPOS
	ADD SI,2
	MOV AX,CX
	MOV [SI],AX
	ADD SI,2

	POP CX

	MOV BX,CX
	MOV CX,0
	MOV DX,MPOSDIR
	JMP PART1


PART1:
	AND DX,3
	CMP DX,0
	JE MTORIGHT1
	CMP DX,2
	JE MTOLEFT1
	CMP DX,1
	JE MTOUP1
	CMP DX,3
	JE MTODOWN1

MTORIGHT1:
	INC AX
	JMP LASTPART1
MTOLEFT1:
	DEC AX
	JMP LASTPART1
MTOUP1:
	SUB AX,256
	JMP LASTPART1
MTODOWN1:
	ADD AX,256
	JMP LASTPART1
LASTPART1:
	MOV [SI],AX
	ADD SI,2
	INC CX	
	CMP CX,4
	JNE PART1

	PUSH SI
	LEA SI,MPOSDIR
	ADD SI,2
	MOV DX,[SI]
	POP SI

PART2:
	AND DX,3
	CMP DX,0
	JE MTORIGHT2
	CMP DX,2
	JE MTOLEFT2
	CMP DX,1
	JE MTOUP2
	CMP DX,3
	JE MTODOWN2
MTORIGHT2:
	INC AX
	JMP LASTPART2
MTOLEFT2:
	DEC AX
	JMP LASTPART2
MTOUP2:
	SUB AX,256
	JMP LASTPART2
MTODOWN2:
	ADD AX,256
	JMP LASTPART2
LASTPART2:
	MOV [SI],AX
	ADD SI,2
	INC CX	
	CMP CX,10
	JNE PART2

	PUSH SI
	LEA SI,MPOSDIR
	ADD SI,4
	MOV DX,[SI]
	POP SI
        JMP PART3

TOBEGINNEWM:
	JMP BEGINNEWM


PART3:
	AND DX,3
	CMP DX,0
	JE MTORIGHT3
	CMP DX,2
	JE MTOLEFT3
	CMP DX,1
	JE MTOUP3
	CMP DX,3
	JE MTODOWN3
MTORIGHT3:
	INC AX
	JMP LASTPART3
MTOLEFT3:
	DEC AX
	JMP LASTPART3
MTOUP3:
	SUB AX,256
	JMP LASTPART3
MTODOWN3:
	ADD AX,256
	JMP LASTPART3
LASTPART3:
	MOV [SI],AX
	ADD SI,2
	INC CX	
	CMP CX,14
	JNE PART3


	LEA SI,MPOS
	MOV BX,[SI]
	MOV DX,0
	MOV NEEDCKM,0
LOOPCHECKM:
	ADD SI,2
	MOV CX,[SI]
	CMP CH,1
	JLE TOBEGINNEWM
	CMP CH,23
	JGE TOBEGINNEWM
	CMP CL,1
	JLE TOBEGINNEWM
	CMP CL,78
	JGE TOBEGINNEWM
	PUSH SI
	PUSH DX
	CALL CHECKPOS
	POP DX
	POP SI
	CMP AX,1
	JNE TOBEGINNEWM
	INC DX
	CMP DX,14
	JNE LOOPCHECKM

	RET
NEWM ENDP


CHECKPOS PROC NEAR
	CMP CH,0
        JLE TORETFALSE
	CMP CH,24
        JGE TORETFALSE
	CMP CL,0
        JLE TORETFALSE
	CMP CL,79
        JGE TORETFALSE
	CMP BL,FACE
	JNE NOTSAMEFACE
	CMP HEAD,CX
	JE TORETFALSE
	CMP TAIL,CX
	JE TORETFALSE
	LEA SI,SNAKE
	MOV AX,0
        JMP CMPSNAKE
TORETFALSE:
        JMP RETFALSE
CMPSNAKE:
	MOV DX,[SI]
	CMP DX,CX
	JE RETFALSE
	ADD AX,2
	ADD SI,2
	CMP AX,SLEN
	JNE CMPSNAKE
NOTSAMEFACE:
	LEA SI,APOS
	ADD SI,BX
	ADD SI,BX
	CMP CX,[SI]
	JE RETFALSE
	CMP BX,HPOS
	JNE NOTSAMEFACEASH
	LEA SI,HPOS
	ADD SI,2
	CMP CX,[SI]
	JE RETFALSE

NOTSAMEFACEASH:
	CMP NEEDCKM,0
	JE NOTSAMEFACEASM
	CMP BX,MPOS
	JNE NOTSAMEFACEASM
	MOV AX,0
	LEA SI,MPOS
CMPM:
	ADD SI,2
	CMP CX,[SI]
	JE RETFALSE
	INC AX
	CMP AX,14
	JNE CMPM
NOTSAMEFACEASM:
	MOV AX,BX
	PUSH BX
	MOV BX,CX
	PUSH CX
	MOV CX,0
	MOV DX,604
	CMP AX,0
	JE AXIS0
	MOV DX,678
AXIS0:
	MOV AH,AL
	MOV AL,FACE
	MOV FACE,AH
        PUSH AX
	CALL BISEARCH
	MOV DX,AX
        POP AX
	MOV FACE,AL
	POP CX
	POP BX
	CMP DX,0FFFFH
	JNE RETFALSE
RETTRUE:
	MOV AX,1
	RET
RETFALSE:
	MOV AX,0
	RET

CHECKPOS ENDP

RAND PROC NEAR
	PUSH CX
        MOV AH,2CH
        INT 21H
        ADD CH,DL
        MOV AX,CX
        ADD AX,RANTIME
        INC RANTIME
        MOV CH,137
        IMUL CH
        SUB DX,CX
        MOV AX,DX
        SUB DX,DX
        IDIV BX
        MOV AX,DX
        MOV AH,0
	POP CX
        RET
RAND ENDP

PAUSE PROC NEAR
	MOV AH,0CH
        MOV AL,0
        INT 21H
WAITSPACE:
	MOV AH,86H
	MOV CX,0
	MOV DX,500
	INT 15H
        CALL SHOWSCORE
        MOV AH,1
	INT 16H
	JZ WAITSPACE
	CMP AL,27
	JNE IFSPACE
	CALL EXIT
IFSPACE:
	CMP AL,' '
	JE PAUSEBREAK
	MOV AH,0CH
        MOV AL,0
        INT 21H
	JMP WAITSPACE
PAUSEBREAK:
	MOV AH,0CH
        MOV AL,0
        INT 21H
	RET
PAUSE ENDP

GAMEOVER PROC NEAR
        MOV BH,0
	MOV AH,2
	MOV DX,3094
	INT 10H
	MOV AH,9
	MOV CX,36
	MOV AL,' '
	MOV BL,0F0H
	INT 10H
	MOV DX,3094
	MOV AH,2
	INT 10H
	MOV DX,OFFSET LOSER	
	MOV AH,9
	INT 21H
        CALL SHOWHEALTH
	MOV DX,0
        MOV AH,86H
        MOV CX,5
        MOV DX,0
        INT 15H
        MOV AH,0CH
        MOV AL,0
        INT 21H        
WAITFORKEY:
        MOV AH,86H
        MOV CX,0
        MOV DX,500
        INT 15H
        CALL SHOWSCORE
	MOV AH,1
	INT 16H
	JZ WAITFORKEY
	MOV AH,0CH
        MOV AL,0
        INT 21H
	CALL EXIT
	RET
GAMEOVER ENDP

EXIT PROC NEAR
        CLI
        MOV HEALTH,0
        CALL SHOWHEALTH
	MOV DX,0
EXITCLRSCR:
	MOV BH,0
	MOV AH,2
	INT 10H
	ADD DX,256
	MOV BL,0FH
	MOV AH,9
	MOV CX,80
	MOV AL,' '
	INT 10H
	CMP DH,25
	JNE EXITCLRSCR
	MOV DX,0
	MOV BH,0
	MOV AH,2
	INT 10H
        MOV DX,0E48AH
        MOV AL,0
        OUT DX,AL
        MOV AH,4CH
        INT 21H
        RET
EXIT ENDP

SHOWHEALTH PROC NEAR
        MOV BX,HEALTH
        CMP BX,8
        JG TOMUCHHEALTH
        JMP INOUT
TOMUCHHEALTH:
        MOV BL,8
INOUT:
        MOV BH,0
        LEA SI,HEALSW
        ADD BX,BX
        ADD SI,BX
        MOV AX,[SI]
        MOV DX,0E489H
        OUT DX,AL
        RET
SHOWHEALTH ENDP

SHOWSCORE PROC NEAR
	PUSH SI
        CMP SHOWOD,0
        JE TOSHOW0
        CMP SHOWOD,1
        JE TOSHOW1
        CMP SHOWOD,2
        JE TOSHOW2
        CMP SHOWOD,3
        JE TOSHOW3
        CMP SHOWOD,4
        JE TOSHOW4
        CMP SHOWOD,5
        JE TOSHOW5
TOSHOW0:
        JMP FAR PTR SHOW0
TOSHOW1:
        JMP FAR PTR SHOW1
TOSHOW2:
        JMP FAR PTR SHOW2
TOSHOW3:
        JMP FAR PTR SHOW3
TOSHOW4:
        JMP FAR PTR SHOW4
TOSHOW5:
        JMP FAR PTR SHOW5

SHOW0:
        MOV AX,SCOREL
        MOV DX,0
        MOV BX,10
        IDIV BX
        MOV CX,DX
        MOV BX,AX
        MOV DX,0E48AH
        MOV AL,7
        OUT DX,AL
        MOV DX,0E488H
        LEA SI,LED
        ADD SI,CX
        MOV AL,[SI]
        OUT DX,AL
        JMP FAR PTR RETURN

SHOW1:
        MOV AX,SCOREL
        MOV DX,0
        MOV BX,10
        IDIV BX
        MOV DX,0
        IDIV BX
        MOV CX,DX
        MOV BX,AX
        MOV DX,0E48AH
        MOV AL,11
        OUT DX,AL
        MOV DX,0E488H
        LEA SI,LED
        ADD SI,CX
        MOV AL,[SI]
        OUT DX,AL
        JMP FAR PTR RETURN
SHOW2:
        MOV AX,SCOREL
        MOV DX,0
        MOV BX,10
        IDIV BX
        MOV DX,0
        IDIV BX
        MOV CX,AX
        MOV BX,AX
        MOV DX,0E48AH
        MOV AL,19
        OUT DX,AL
        MOV DX,0E488H
        LEA SI,LED
        ADD SI,CX
        MOV AL,[SI]
        OUT DX,AL
        JMP FAR PTR RETURN

SHOW3:
        MOV AX,SCOREH
        MOV DX,0
        MOV BX,10
        IDIV BX
        MOV CX,DX
        MOV BX,AX
        MOV DX,0E48AH
        MOV AL,35
        OUT DX,AL
        MOV DX,0E488H
        LEA SI,LED
        ADD SI,CX
        MOV AL,[SI]
        OUT DX,AL
        JMP FAR PTR RETURN

SHOW4:
        MOV AX,SCOREH
        MOV DX,0
        MOV BX,10
        IDIV BX
        MOV DX,0
        IDIV BX
        MOV CX,DX
        MOV BX,AX
        MOV DX,0E48AH
        MOV AL,67
        OUT DX,AL
        MOV DX,0E488H
        LEA SI,LED
        ADD SI,CX
        MOV AL,[SI]
        OUT DX,AL
        JMP FAR PTR RETURN
SHOW5:
        MOV AX,SCOREH
        MOV DX,0
        MOV BX,10
        IDIV BX
        MOV DX,0
        IDIV BX
        MOV CX,AX
        MOV BX,AX
        MOV DX,0E48AH
        MOV AL,131
        OUT DX,AL
        MOV DX,0E488H
        LEA SI,LED
        ADD SI,CX
        MOV AL,[SI]
        OUT DX,AL
        JMP FAR PTR RETURN
RETURN:
        INC SHOWOD
        CMP SHOWOD,6
        JE TOZERO
        JMP LEAVE
TOZERO:
        MOV SHOWOD,0
LEAVE:
        POP SI
	RET
SHOWSCORE ENDP


SOUND PROC NEAR
        PUSH AX
        PUSH BX
        PUSH CX
        PUSH DX
        PUSH DI
        MOV AL,0B6H
        OUT 43H,AL
        MOV DX,12H
        MOV AX,34DCH
        DIV DI
        OUT 42H,AL
        MOV AL,AH
        OUT 42H,AL
        IN AL,61H
        MOV AH,AL
        OR AL,3
        OUT 61H,AL
DELAYTIME:
        MOV CX,12000
DLMS:
        LOOP DLMS
        DEC BX
        JNZ DELAYTIME
        MOV AL,AH
        OUT 61H,AL
        POP DI
        POP DX
        POP CX
        POP BX
        POP AX
        RET
SOUND ENDP


IRQ PROC FAR
        PUSH AX
        CMP ALLOWI,1
        JE NOTPUSHBX
        PUSH BX
NOTPUSHBX:
        PUSH CX
        PUSH DX
        MOV BH,0
        MOV AL,20H
        OUT 20H,AL
        MOV DX,0EC4DH
        MOV AL,1DH
        OUT DX,AL
        POP DX
        POP CX
        CMP ALLOWI,1
        JE NOTPOPBX
        POP BX
NOTPOPBX:
        POP AX
        IRET
IRQ ENDP


CODE ENDS
END START
